<template>
  <div class="upload">
    <el-upload
      v-model:file-list="fileList"
      class="upload-demo"
      action="https://run.mocky.io/v3/9d059bf9-4660-45f2-925d-ce80ad6c4d15"
      :multiple="false"
      :on-preview="handlePreview"
      :on-remove="handleRemove"
      :before-remove="beforeRemove"
      :before-upload="beforeUpload"
      :on-success="uploadSuccess"
      :on-error="uploadError"
      :limit="1"
      :on-exceed="handleExceed"
      :headers="{}"
      :data="{}"
    >
      <el-button type="primary" plain>上传附件</el-button>
      <template #tip>
        <div class="el-upload__tip">
          <!-- 仅限上传XXX、XXX格式文件，且文件大小不超过XXM。 -->
        </div>
      </template>
    </el-upload>
  </div>
</template>

<script setup lang="ts">
  import { ref } from 'vue'
  import { ElMessage, ElMessageBox, ElNotification } from 'element-plus'
  import type { UploadProps, UploadUserFile } from 'element-plus'

  const fileList = ref<UploadUserFile[]>([
    {
      name: 'element-plus-logo.svg',
      url: 'https://element-plus.org/images/element-plus-logo.svg',
    },
    {
      name: 'element-plus-logo2.svg',
      url: 'https://element-plus.org/images/element-plus-logo.svg',
    },
  ])

  const handleRemove: UploadProps['onRemove'] = (file, uploadFiles) => {
    console.log(file, uploadFiles)
  }

  const handlePreview: UploadProps['onPreview'] = (uploadFile) => {
    console.log(uploadFile)
  }

  const handleExceed: UploadProps['onExceed'] = (files, uploadFiles) => {
    ElMessage.warning(
      `The limit is 3, you selected ${files.length} files this time, add up to ${
        files.length + uploadFiles.length
      } totally`
    )
  }

  const beforeUpload: UploadProps['beforeUpload'] = (rawFile) => {
    const imgSize = rawFile.size / 1024 / 1024 < props.fileSize
    let imgType = props.fileType.includes(rawFile.type as File.ImageMimeType)
    imgType = true
    if (!imgType)
      ElNotification({
        title: '温馨提示',
        message: '上传图片不符合所需的格式！',
        type: 'warning',
      })
    if (!imgSize)
      setTimeout(() => {
        ElNotification({
          title: '温馨提示',
          message: `上传图片大小不能超过 ${props.fileSize}M！`,
          type: 'warning',
        })
      }, 0)
    return imgType && imgSize
  }

  const beforeRemove: UploadProps['beforeRemove'] = (
    uploadFile,
    uploadFiles
  ) => {
    return ElMessageBox.confirm(
      `Cancel the transfer of ${uploadFile.name} ?`
    ).then(
      () => true,
      () => false
    )
  }

  const emeit = defineEmits<{
    'upload:Success': (data: UploadUserFile[]) => void
  }>()

  /**
   * @description 图片上传成功
   * */
  const uploadSuccess = (
    response: { fileUrl: string } | undefined,
    uploadFile: UploadFile
  ) => {
    fileList.value.push({
      name: '',
      url: '',
    })

    emeit('upload:Success', fileList.value)

    ElNotification({
      title: '温馨提示',
      message: '文件上传成功！',
      type: 'success',
    })
  }

  /**
   * @description 图片上传错误
   * */
  const uploadError = () => {
    ElNotification({
      title: '温馨提示',
      message: '文件上传失败，请您重新上传！',
      type: 'error',
    })
  }
</script>

<style scoped lang="scss">
  .upload-demo {
    position: relative;
    :deep(.el-upload-list) {
      display: flex;
    }
    :deep(.el-upload-list__item) {
      width: 390px;
    }
    :deep(.el-upload__tip) {
      position: absolute;
      left: 112px;
      top: 0;
      font-size: 12px;
      color: #00000040;
    }
  }
</style>
